home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Beginner need help??????????????
- Date: 9 Apr 1996 22:02 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <9APR199622022758@erich.triumf.ca>
- References: <4kem82$5j3@dewey.csun.edu>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4kem82$5j3@dewey.csun.edu>, kc44097@csun.edu (chen) writes...
- >
- > I am a new comer of C language and I use Boland C++ 3.1 as
- >my compiler.These days I found some wierd things happens.I do not
- >-------- program 1 -------------------------
- >#include <stdio.h>
- >float answer;
- >main ()
- >{
- > answer = 1 / 3;
- > printf("The value of 1/3 is %f\n",
- > answer);
- > return(0);
- >}
- >????? The answer should be 0.333333 but compiler give me 0.000000 ??????
-
- Since 1 and 3 are int constants, the division will be done as an integer
- operation - with a whole number result - no fractional part. This integer
- result is then converted to a float and assigned to answer.
-
- One way to make this work is to make one or both the operands into floats,
- thus:
- answer = 1.0 / 3.0;
-
-
- >----------program 2----------------------------
- >#include <stdio.h>
- >float result;
- >main()
- >{
- > result = 7.0 / 22.0;
- > printf("The result is %d\n", result);
- > return (0);
- >}
- >???? The answer should be 0.31818 but compiler give me 0 ????
-
- If you lie to the compiler it _will_ get it's revenge!
-
- By using "%d", you have told the compiler you are passing an int, but result is
- really a float. The results of this are undefined - they depend on the format
- and byte order of the internal double (the float will be "promoted" to double
- to be passed to printf()) and int variables.
-
- >----------program 3---------------------------
- >#include <stdio.h>
- >int integer;
- >float floating;
- >main()
- >{
- > floating = 7.0 / 22.0;
- > integer = floating;
- > printf("The value of integer is %f\n", integer);
- > return (0);
- >}
- >???? The answer should be 0.31818 but compiler give me 0.000000 ?????
-
- You lie to the compiler again, and again it takes revenge!
-
- Even if you didn't lie, this would not give the expected result. An int can
- only handle whole numbers. When you do:
- integer = floating;
- the program will assign the integer part of floating (which is 0) to integer.
-
- You then tell printf() (with "%f") that you are passing a float, but actually
- pass an int - printf() will try to convert that int, probably plus a few more
- bytes from the stack, as if it was a double, and print the result. Once again,
- the results are undefined, and rarely desireable.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
- or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
-
-
-
-
-
-
-
-
-